#include "ts.h"

#define K_Value 1730;

float TU = 0.0;
float TU_value = 0.0;
float TU_calibration = 0.0;

#ifdef ENABLE_TS
void setup()
{
    Serial.begin(115200);
}
void loop()
{
    float temp = 25;
    Serial.print(get_ntu(temp));
    delay(1000);
}

#endif
void read_ntu(float temp_dirty, float temp_clear, float *ntu_dirty, float *ntu_clear)
{
    uint16_t read_value = analogRead(TS_PIN_DIRTY);
    float TU_d = (float)read_value * (5.0 / 4095);
    Serial.println(TU_d);
    TU_calibration = -0.0192 * (temp_dirty - 25) + TU_d;
    *ntu_dirty = -865.68 * TU_calibration + K_Value;
    if (*ntu_dirty <= 0)
        *ntu_dirty = 0;
    if (*ntu_dirty >= 3000)
        *ntu_dirty = 3000;

    read_value = analogRead(TS_PIN_CLEAR);
    float TU_c = (float)read_value * (5.0 / 4095);
    TU_calibration = -0.0192 * (temp_clear - 25) + TU_c;
    *ntu_clear = -865.68 * TU_calibration + K_Value;
    if (*ntu_clear <= 0)
        *ntu_clear = 0;
    if (*ntu_clear >= 3000)
        *ntu_clear = 3000;
}